<?php


namespace app\mingteng\controller;
use think\AjaxPage;
use think\Db;

class Package extends Base
{

    public $listRows = 20;
    public function index(){
        return $this->fetch();
    }

    public function ajax_index(){
        $params = input('post.');
        $page = input('page', '1');

        $where = array();
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $where['packageName'] = array('like', '%'.$params['keywords'].'%');
        }
        $where['packageDel'] = 0;
        $count = Db::name('package')->where($where)->count();
        $list = Db::name('package')->where($where)->page($page, $this->listRows)->select();
        if($list){
            foreach($list as $k=>$v){
                $list[$k]['addTime'] = date('Y-m-d H:i',$v['addTime']);
            }
        }
        $this->assign('list', $list);

        $ajaxPage = new AjaxPage($count, $this->listRows);
        $show = $ajaxPage->show();
        $this->assign('page', $show);

        return $this->fetch();
    }

    public function add(){

        $package = config('package');

        $this->assign('package', $package);
        return $this->fetch();
    }

    public function doAdd(){
        $params = input('post.');

        if (empty($params['packageName'])) {
            return json(array('code' => 500, 'msg' => '请输入套餐名称'));
        }
        if (empty($params['main_key'])) {
            return json(array('code' => 500, 'msg' => '请选择主营套餐'));
        }
        if (empty($params['price']) || empty($params['discountedPrice'])) {
            return json(array('code' => 500, 'msg' => '请输入价格'));
        }

        $post = $this->useData($params);

        $mainPackage = [
            'main_key' => $params['main_key'],
            'main_value' => $params['main_value'],
        ];

        $data = [
          'packageName' => $params['packageName'],
          'mainPackage' => json_encode($mainPackage),
          'matchingPackage' => json_encode($post),
          'price' => $params['price'],
          'discountedPrice' => $params['discountedPrice'],
          'addTime' => time(),
          'logs' => $params['logs'],
          'describe' => $params['describe'],
        ];

        $one = Db::name('package')->insert($data);

        return json(array('code' => 200, 'msg' => '添加成功','data'=>$one));
    }

    /**
     * 处理数据
     * @param $params
     * @return array
     */
    public function useData($params){

        $gear_key = $params['gear_key'];
        $gear_value = $params['gear_value'];

        $data = [];
        foreach ($gear_key as $key=>$vo)
        {
            $val['gear_key']=$vo;
            $val['gear_value']=$gear_value[$key];
            $data[] = $val;
        }
        return $data;
    }

    public function edit(){
        $packageId = input('packageId');

        $data= Db::name('package')->where('packageId', '=', $packageId)->find();
        $data['mainPackage'] = json_decode($data['mainPackage'],true);
        $data['matchingPackage'] =json_decode($data['matchingPackage'],true);
        $data['matchingPackageCount'] = count($data['matchingPackage']);
        $data['matchingPackageJson'] =json_encode($data['matchingPackage']);
        $package = config('package');
        $this->assign('package', $package);
        $this->assign('data', $data);
        $this->assign('packageId', $packageId);

        return $this->fetch();
    }

    public function doEdit(){
        $params = input('post.');
        if(empty($params['packageId'])){
            return json(array('code' => 500, 'msg' => '参数错误'));
        }
        if (empty($params['packageName'])) {
            return json(array('code' => 500, 'msg' => '请输入套餐名称'));
        }
        if (empty($params['main_key'])) {
            return json(array('code' => 500, 'msg' => '请选择主营套餐'));
        }
        if (empty($params['price'])) {
            return json(array('code' => 500, 'msg' => '请输入价格'));
        }

        $post = $this->useData($params);

        $mainPackage = [
            'main_key' => $params['main_key'],
            'main_value' => $params['main_value'],
        ];

        $data = [
            'packageName' => $params['packageName'],
            'mainPackage' => json_encode($mainPackage),
            'matchingPackage' => json_encode($post),
            'price' => $params['price'],
            'discountedPrice' => $params['discountedPrice'],
            'logs' => $params['logs'],
            'describe' => $params['describe'],
        ];

        $one = Db::name('package')->where('packageId',$params['packageId'])->update($data);

        return json(array('code' => 200, 'msg' => '添加成功','data'=>$one));
    }

    public function del(){
        $del_id = input('del_id');

        Db::name('package')->where('packageId', '=', $del_id)->update(['packageDel'=>1]);
        return json(array('code' => 200, 'msg' => '操作成功'));
    }

    public function  classification(){

        $main_key = input('get.main_key');
        $data=[];
        if($main_key == 1){
            //会员
            $data = [
                ['id'=>1,'value'=>'月度会员'],
                ['id'=>2,'value'=>'年度会员'],
            ];
        }
        if($main_key == 2){
            //会员抵扣券
            $data = Db::name('voucher')->field('voucherId as id,voucherName as value')->select();
        }
        if($main_key == 3){
            //充值卡
            $data = [
              ['id'=>1,'value'=>1000],
              ['id'=>2,'value'=>2000],
              ['id'=>3,'value'=>5000],
              ['id'=>4,'value'=>10000],
            ];
        }
        if($main_key == 4){
            //课程
            $data = Db::name('coach_course')->where('courseIsDel',0)->field('courseId as id,courseTitle as value')->select();
        }
        if($main_key == 5){
            //物品
            $goods_category = Db::name('goods_category')->where('parent_id',1)->column('id');
            if($goods_category){
                $data = Db::name('goods')->where('cat_id','in',$goods_category)->field('goods_id as id,goods_name as value')->select();
            }
        }

        return json(array('code' => 200, 'msg' => '成功','data'=>$data));
    }

    public function packages(){
        $package = config('package');
        return json(array('code' => 200, 'msg' => '成功','data'=>$package));
    }
}